package com.mfcxy.admin.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mfcxy.entity.User;
import com.mfcxy.entityvo.UserAddVo;
import com.mfcxy.exception.CustomException;
import com.mfcxy.service.IUserService;

/**
 * 后台登陆或者退出
 * @author mfcxy
 *
 */
@Controller
@RequestMapping("/admin/login/")
public class LoginController {
	
	@Autowired
	private IUserService userService;
	
	/**
	 * 登陆视图
	 * @return
	 */
	@RequestMapping("loginview" )
	public String loginView() {
		return "admin/login_login";
	}
	
	
	
	/**
	 * 执行登陆操作
	 *  method=RequestMethod.POST 这个属性代表 限制post提交方式
	 * @throws CustomException 
	 */
	@RequestMapping(value = "login" , method=RequestMethod.POST)
	public String login(@ModelAttribute UserAddVo user , HttpServletRequest request) throws CustomException {
		//验证提交的登陆数据
		user.checkLogin();
		if(userService.Login(user)) {
			return "redirect:/admin/";
		} else {
			throw new CustomException("用户名或者密码错误");
		}
	}
	
	/**
	 * 退出
	 */
	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		//清理session
		request.getSession().setAttribute("user", null);
		return "redirect:/admin/login/loginview";
	}
}
